package com.wuliu.controller;

import com.wuliu.common.annotation.Log;
import com.wuliu.common.core.controller.BaseController;
import com.wuliu.common.core.domain.AjaxResult;
import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.core.domain.PageResult;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.core.page.TableDataInfo;
import com.wuliu.common.enums.BusinessType;
import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.poi.ExcelUtil;
import com.wuliu.system.domain.BasAddress;
import com.wuliu.system.domain.dto.app.BasAddressDto;
import com.wuliu.system.service.IBasAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户地址库Controller
 *
 * @author realue
 * @date 2023-10-28
 */
@Api(tags = {"用户地址库控制器"})
@RestController
@RequestMapping("/app/address")
public class BasAddressController extends BaseController {
    @Resource
    private IBasAddressService basAddressService;

    /**
     * 查询用户地址库列表
     */
    @ApiOperation(value = "查询")
    @GetMapping("/list")
    public ApiResult<PageResult<BasAddress>> list(BasAddressDto dto) {
        return ApiResult.success(basAddressService.selectList(dto));
    }

    /**
     * 获取用户地址库详细信息
     */
    @ApiOperation(value = "详细")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(basAddressService.selectBasAddressById(id));
    }

    /**
     * 新增用户地址库
     */
    @ApiOperation(value = "新增")
    @Log(title = "用户地址库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated BasAddress basAddress) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        basAddress.setSysAccount(user.getSysAccount());
        basAddress.setUserId(user.getUserId());
        basAddress.setCreateBy(user.getUserName());
        return toAjax(basAddressService.insertBasAddress(basAddress));
    }

    /**
     * 修改用户地址库
     */
    @ApiOperation(value = "编辑")
    @Log(title = "用户地址库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated BasAddress basAddress) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        basAddress.setUpdateBy(user.getUserName());
        return toAjax(basAddressService.updateBasAddress(basAddress));
    }

    /**
     * 删除用户地址库
     */
    @ApiOperation(value = "删除")
    @Log(title = "用户地址库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(basAddressService.deleteBasAddressByIds(ids));
    }

    @ApiOperation(value = "设为默认地址")
    @Log(title = "用户地址库", businessType = BusinessType.DELETE)
    @PostMapping("/setDefault/{id}")
    public ApiResult<Boolean> setDefault(@PathVariable("id") String id){
        return ApiResult.success(basAddressService.setDefault(id));
    }
}
